﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace QuaManSys
{
    public partial class MainPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            //判断用户是否是非法访问
            if (Session["CurUser"] != null)
            {
                //从session中取出当前用户信息并显示
                Tab_Person user = (Tab_Person)Session["CurUser"];
                lblUserName.Text = user.name;
                lblUserDepartment.Text = user.department;
                ShowReminder(user);
            }
            else
            {
                //跳转到登录页面
                Response.Redirect("~/Login.aspx");
            }
        }
        //注销
        protected void LinkButtonOut_Click(object sender, EventArgs e)
        {
            //清空session中的数据
            Session["CurUser"] = null;
            Response.Redirect("~/Login.aspx");
        }

        protected void btnInner_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/InnerInfo/InnerMainPage.aspx");
        }
        /// <summary>
        /// 根据不同的登录身份显示不同的消息栏
        /// </summary>
        /// <param name="user"></param>
        private void ShowReminder(Tab_Person user)
        {
            string dept = user.department;
            int innerTotalItem = 0, outerTotalItem = 0;
            if ("QA" == dept)
            {
                innerTotalItem = CommonFunctions.QueryInnerStatusDis() + 
                    CommonFunctions.QueryInnerStatusPro(dept);
                outerTotalItem = CommonFunctions.QueryOuterStatusDis() +
                    CommonFunctions.QueryOuterStatusPro(dept);
            }
            else
            {
                innerTotalItem = CommonFunctions.QueryInnerStatusPro(dept);
                outerTotalItem = CommonFunctions.QueryOuterStatusPro(dept);
            }
            DisplayReminderList(innerTotalItem, outerTotalItem);
        }

        private void DisplayReminderList(int inTotalItem, int outTotalItem)
        {
            int totalItem = inTotalItem + outTotalItem;
            if (totalItem != 0)
            {
                if (inTotalItem != 0)
                {
                    HyperLink innerHref = new HyperLink();
                    pnlReminder.Controls.Add(innerHref);
                    innerHref.Text = "您共有" + inTotalItem.ToString() +
                        "条内部质量信息需要处理。";
                    innerHref.NavigateUrl = "InnerInfo/InnerMainPage.aspx";
                    pnlReminder.Controls.Add(new LiteralControl("<br />"));
                } 
                else 
                {
                    Label myLabel = new Label();
                    myLabel.Text = "恭喜！您当前内部信息已经处理完毕！请及时处理外部信息";
                    pnlReminder.Controls.Add(myLabel);
                    pnlReminder.Controls.Add(new LiteralControl("<br />"));
                }
                if (outTotalItem != 0)
                {
                    HyperLink outerHref = new HyperLink();
                    pnlReminder.Controls.Add(outerHref);
                    outerHref.Text = "您共有" + outTotalItem.ToString() +
                        "条外部质量信息需要处理。";
                    outerHref.NavigateUrl = "OuterInfo/OuterMainPage.aspx";
                    pnlReminder.Controls.Add(new LiteralControl("<br />"));
                } 
                else
                {
                    Label myLabel = new Label();
                    myLabel.Text = "恭喜！您当前外部信息已经处理完毕！请及时处理内部信息";
                    pnlReminder.Controls.Add(myLabel);
                    pnlReminder.Controls.Add(new LiteralControl("<br />"));
                }
            }
            else
            {
                Label myLabel = new Label();
                myLabel.Text = "恭喜！您当前所有信息已经处理完毕！";
                pnlReminder.Controls.Add(myLabel);
                pnlReminder.Controls.Add(new LiteralControl("<br />"));
            }
        }

        protected void btnOuter_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/OuterInfo/OuterMainPage.aspx");
        }
    }
}